from .commAPI import *
from .models import *
from .DB_API import *
import sys

sys.path.append('.')
from . import elineStat, eTeachOption


class businessAPI():
    def __init__(self):
        pass

    # 添加课程
    '''
        函数名:addCourse
        描述：添加课程
        参数：name:课程名称，disc：课程描述，logo：课程图片,label:课程标签-列表, oldID:修改课程需要传入修改课程的id
        返回值 None
        '''

    def addCourse(self, name, disc, logo, label, oldID=None):
        dboption = cDBtoDo()
        courseid = dboption.addCourse(name, disc, logo, oldID)
        for x in label:
            dboption.addRelation_CourseLabel(courseid, x)
        return courseid

    # 删除课程
    '''
        函数名:delCourse
        描述：删除课程
        参数：id：课程id
        返回值 None
        '''

    def delCourse(self, id):
        dboption = cDBtoDo()
        course = dboption.delCourse(id)
        return course

    '''
        函数名:adminExamCourse
        描述：审核课程
        参数：id：课程id，flag：可用标志
        返回值 课程
        '''

    def adminExamCourse(self, id, flag):
        dboption = cDBtoDo()
        item = dboption.getCourseByID(id)

        if flag == elineStat.refuce:
            item.flag = flag
            return
        if item.option == eTeachOption.add and flag == elineStat.enable:
            dboption.setFlagCourse(item.oldID, elineStat.disable)
            dboption.setFlagCourse(id, flag)
            # 更新章节的所属课程id
            chapterlist = dboption.getChapterByCourseID(item.oldID)
            for x in chapterlist:
                dboption.setCourseIDChapter(x.id, id)  # 更新已有章节的课程id
                if x.flag == elineStat.examine:
                    dboption.setFlagChapter(x.id, flag)  # 设置课程相关的章节，如果需要审批则一并审批
            # 通过相关标签
            '''标签不管，默认添加课程时就可用'''
            return id

        if item.option == eTeachOption.delete and flag == elineStat.enable:
            dboption.setFlagCourse(id, flag)
            # 设置相关章节为不可用
            chapterlist = dboption.getChapterByCourseID(item.oldID)
            for x in chapterlist:
                dboption.setFlagChapter(x.id, elineStat.disable)
                if x.flag == elineStat.examine or x.flag == elineStat.enable:
                    dboption.setFlagChapter(x.id, elineStat.disable)  # 设置课程相关的章节，如果需要审批则一并审批

            # 删除标签
            '''标签不管'''
            return id

    '''
        函数名:addChapter
        描述：为课程添加章节
        参数：name：章节名称,courseid：课程id,disc：章节描述,modelid：模板id,level：章节顺序
        返回值 章节id
        '''

    def addChapter(self, name, courseid, disc, modelid, level):
        dboption = cDBtoDo()
        id = dboption.addChapter(name=name, courseid=courseid, disc=disc, modelid=modelid, level=level)
        return id

    '''
        函数名:adminExamChapter
        描述：删除课程
        参数：id：课程id，flag：可用标志
        返回值 课程
        '''

    def adminExamChapter(self, id, flag):
        dboption = cDBtoDo()
        retchapter = dboption.getChapterByID(id)
        if flag == elineStat.enable:
            # 审核章节模板
            ''''''
            # 审核章节
            ''''''
        id = dboption.setFlagChapter(id, flag)
        return id

    # 添加新标签
    '''
        函数名:addLabel
        描述：添加课程
        参数：name:标签名称
        返回值 None
        '''

    def addLabel(self, name):
        dboption = cDBtoDo()
        labelid = dboption.addLabel(name)
        return labelid


busiOBJ = businessAPI()
